package aop3;

import java.lang.reflect.Method;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-10 09:25
 * @since JDK 1.6
 */
public class Aop {

    private static CrossCutting cut=new CrossCutting();
    private static ReflectUtils rfBefore=new ReflectUtils(cut,"before");
    private static ReflectUtils rfAfter=new ReflectUtils(cut,"after");

    public static void beforeAdvice(Object target,String methodName,Object ... params){
        Class cls=target.getClass();
        Method[] methods=cls.getDeclaredMethods();
        for(int i=0;i<methods.length;i++){
            if(methods[i].getName().equals(methodName)){
                rfBefore.invokeMethod();
                new ReflectUtils(target,methodName,params).invokeMethod();
            }
        }
    }

    public static void afterAdvice(Object target,String methodName,Object... params){
        Class cls=target.getClass();
        Method[] methods=cls.getDeclaredMethods();
        for(int i=0;i<methods.length;i++){
            if(methods[i].getName().equals(methodName)){
                new ReflectUtils(target,methodName,params).invokeMethod();
                rfAfter.invokeMethod();

            }
        }
    }

    public static void aroundAdvice(Object target,String methodName,Object... params){
        Class cls=target.getClass();
        Method[] methods=cls.getDeclaredMethods();
        for(int i=0;i<methods.length;i++){
            if(methods[i].getName().equals(methodName)){
                rfBefore.invokeMethod();
                new ReflectUtils(target,methodName,params).invokeMethod();
                rfAfter.invokeMethod();

            }
        }
    }

}
